package eng;

import front.Scene;
import java.util.ArrayList;
import java.util.List;
import light.Light;
import photon.PhotonMapper;
import prim.PRect;
import prim.Primitive;
import render.Renderer;

public class EngineBase
{
    
    private PhotonMapper photonMapper;
    
    private Renderer renderer;
    public Renderer getRenderer() { return this.renderer; }
    
    private List<Primitive> listPrimitives;
    
    private List<Light> listLights;
    
    private boolean isRunning;
    
    public EngineBase(
            int windowWidth, 
            int windowHeight, 
            int pixelsWide, 
            int pixelsTall)
    {
        this.photonMapper = new PhotonMapper();
        
        this.renderer = new Renderer(
            windowWidth, 
            windowHeight, 
            pixelsWide, 
            pixelsTall
        );
        
        this.listPrimitives = new ArrayList<>();
        
        this.listLights = new ArrayList<>();
        
        this.isRunning = true;
    }
    
    public final void run()
    {
        this.onStart();
        this.photonMapper.setPrimitives(this.listPrimitives);
        this.photonMapper.setLights(this.listLights);
        this.photonMapper.tracePhotons();
        this.photonMapper.blurColorMaps();
        this.renderer.setPrimitives(this.listPrimitives);
        this.renderer.setLights(this.listLights);
        while (this.isRunning)
        {
            if (this.renderer.isTracingRays())
            {
                this.renderer.updateRayTraces();
            }
            else
            {
                if (!this.listPrimitives.isEmpty())
                {
                    for (Primitive prim : this.listPrimitives)
                    {
                        prim.destroy();
                    }
                }
            }
            this.renderer.drawToPixelBuffer();
            this.renderer.drawToWindow();
        }
        this.renderer.destroy();
    }
    
    protected void onStart()
    {
        
    }
    
    protected final void addScene(Scene scene)
    {
        List<Primitive> list = scene.getPrimitives();
        for (Primitive prim : list)
        {
            this.addPrimitive(prim);
        }
    }
    
    protected final void addPrimitive(Primitive prim)
    {
        this.listPrimitives.add(prim);
    }
    
    protected final void addLight(Light light)
    {
        this.listLights.add(light);
    }
    
}
